﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Clinica_Frba.Generar_Receta
{
    public partial class ListadoRecetas : Form
    {
        public ListadoRecetas()
        {
            InitializeComponent();
        }

        private void btnBuscarRecetas_Click(object sender, EventArgs e)
        {
            string strQuery = "select R.ID_RECETA AS ID_RECETA, PL.Descripcion as PLAN_MEDICO, (P.NOMBRE + ' ' + P.APELLIDO) AS PROFESIONAL, (A.NOMBRE + ' ' + A.APELLIDO) AS AFILIADO, T.FECHA from RECETA R, TURNO T, PLAN_MEDICO PL, PROFESIONAL P, AFILIADO A WHERE R.ID_TURNO = T.ID_TURNO AND R.ID_PROF = P.ID_PROF AND R.ID_AFIL = A.ID_AFIL AND A.ID_PLAN = PL.ID_PLAN  ";

            if (this.comboBox1.SelectedIndex != -1)
            {
                 String idMedico = comboBox1.SelectedValue.ToString();

                 strQuery += " and R.id_prof='" + idMedico + "'";
        
            }

            strQuery += " order by ID_RECETA DESC;";

            System.Data.SqlClient.SqlConnection myConnection = new System.Data.SqlClient.SqlConnection(publico.ConnectionString);
            myConnection.Open();
            System.Data.SqlClient.SqlCommand consulta = new System.Data.SqlClient.SqlCommand(strQuery, myConnection);
            System.Data.SqlClient.SqlDataReader executeCMD = consulta.ExecuteReader();

            dgvResultadoDeBusqueda.Rows.Clear();
            while (executeCMD.Read())
            {
                dgvResultadoDeBusqueda.Rows.Add(executeCMD["ID_RECETA"], executeCMD["PLAN_MEDICO"], executeCMD["PROFESIONAL"], executeCMD["AFILIADO"], executeCMD["FECHA"]);
            }

            if (dgvResultadoDeBusqueda.Rows.Count == 0)
            {
                Dialogos.MostrarMessageDialog("No se encontraron coincidencias...");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            dgvResultadoDeBusqueda.Rows.Clear(); 
        }

        private void ListadoRecetas_Load(object sender, EventArgs e)
        {
            comboBox1.DataSource = LoadDataTableMedicos();
            comboBox1.DisplayMember = "Nombre";
            comboBox1.ValueMember = "Id";

            if (publico.idProfesionalLogueado != null && publico.idProfesionalLogueado.Length > 0)
            {
                comboBox1.SelectedValue = publico.idProfesionalLogueado;
                comboBox1.SelectedIndex = 0;
            }
            else
            {
                comboBox1.SelectedIndex = -1;
            }

            //
            // cargo la lista de items para el autocomplete

            //
            comboBox1.AutoCompleteCustomSource = LoadAutoCompleteMedicos();
            comboBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
            comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;

            
        }

        public static DataTable LoadDataTableMedicos()
        {

            string comando = "select id_prof as Id, (apellido + ', ' + nombre) as Nombre from PROFESIONAL ";

            if (publico.idProfesionalLogueado != null && publico.idProfesionalLogueado.Length > 0)
            {
                comando += " where id_prof = " + publico.idProfesionalLogueado + " ";
            }

            comando += "order by (apellido + ', ' + nombre);";
            System.Data.SqlClient.SqlConnection myConnection = new System.Data.SqlClient.SqlConnection(publico.ConnectionString);
            myConnection.Open();
            System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(comando, myConnection);
            DataTable dt = new DataTable();

            da.Fill(dt);

            return dt;
        }

        public static AutoCompleteStringCollection LoadAutoCompleteMedicos()
        {
            DataTable dt = LoadDataTableMedicos();

            AutoCompleteStringCollection stringCol = new AutoCompleteStringCollection();

            foreach (DataRow row in dt.Rows)
            {
                stringCol.Add(Convert.ToString(row["Nombre"]));
            }

            return stringCol;
        }

        private void dgvResultadoDeBusqueda_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvResultadoDeBusqueda.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex != -1)
            {
                if (dgvResultadoDeBusqueda.Rows[e.RowIndex].Cells["ID_RECETA"].Value != null)
                {
                    ViewRecetas form = new ViewRecetas(dgvResultadoDeBusqueda.Rows[e.RowIndex].Cells["ID_RECETA"].Value.ToString());
                    form.ShowDialog(this);
                }
            }
        }


    }
}
